package com.example.administrator.myapplication;



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;


import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.myapplication.interf.BackHandleInterface;

import java.util.Timer;
import java.util.TimerTask;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SettingsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SettingsFragment extends MyBaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "SettingsFragment";

    private ActionBar actionBar;
    private BackHandleInterface mBackHandleInterface;
    private RelativeLayout gloab_relative;
    private RelativeLayout exit_relative;
    private FragmentManager fragmentManager;
    private Fragment fragment;
    private BottomNavigationView navigation;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public SettingsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SettingsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SettingsFragment newInstance(String param1, String param2) {
        SettingsFragment fragment = new SettingsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(!(getActivity() instanceof BackHandleInterface)){
            throw new ClassCastException(" 父Activity没有实现 BackHandleInterface 接口 ");
        }else {
            this.mBackHandleInterface = ((BackHandleInterface) getActivity());
        }

        navigation = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
        navigation.setVisibility(BottomNavigationItemView.INVISIBLE);

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //设置为true，Fragment中的onOptionsItemSelect()就可以被触发，默认无法触发。
        //注意，如果此时Activity中任然设置了onOptionsItemSelected()的话则其将具有优先级，而Fragment中的方法将被忽略。
        actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setHasOptionsMenu(true);
        fragmentManager = getActivity().getSupportFragmentManager();



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        gloab_relative = ((RelativeLayout) view.findViewById(R.id.gloab_reletive));
        exit_relative = ((RelativeLayout) view.findViewById(R.id.exit_relative));

        gloab_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyBaseApplication.showToastMake("通用设置").show();
                fragment = new GloabFragment();
                //计时器的一种实现方式，第二种是用Handler,效果一样
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.beginTransaction().replace(R.id.content,fragment).addToBackStack(null).commit();
                    }
                },500);


            }
        });

        exit_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick to exit: 要退出吗？");
                ExitFragment exitFragment = new ExitFragment();
                exitFragment.show(fragmentManager,"xxx");


            }
        });
        // Inflate the layout for this fragment
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onStart() {
        mBackHandleInterface.setSelectedFragment(this);
        super.onStart();

    }

    @Override
    public void onResume() {
        getActivity().setTitle(getString(R.string.title_settings));
        super.onResume();
    }

    /**
     * Activity中的onOptionsItemSelected()的执行默认优先于Fragment，且无法重载。
     *@link 参考 http://blog.csdn.net/ican87/article/details/38012563
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                Log.i(TAG,"设置界面的后退软件按钮被触发！");
                //如非特殊需求，否则应使用标准方法实现后退功能。
                getActivity().onBackPressed();
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 当Activity中的onBackPressed()执行时需要执行当前Fragment中的此方法
     */
    public void back(){
        actionBar.setDisplayHomeAsUpEnabled(false);
        navigation.setVisibility(BottomNavigationItemView.VISIBLE);

    }



}
